package server.communication.listeners;

import server.threads.ServerCommunicator;
import message.Message;
import message.PingRequestMessage;
import message.PingServerResponseMessage;
import message.RestartServerRequestMessage;
import message.ServerMessageWrapper;
import message.UpdateDataRequestMessage;
import message.UpdateDataResponseMessage;
import communication.MessageListener;
import communication.MessageReceivedEvent;

public class PingListener implements MessageListener
{
	private final ServerCommunicator communicator;
	
	public PingListener(ServerCommunicator communicator)
	{
		this.communicator = communicator;
	}
	@Override
	public void messageRecieved(MessageReceivedEvent e)
	{
		ServerMessageWrapper message = ((ServerMessageWrapper) e.getMessage());
		PingServerResponseMessage responseMessage = new PingServerResponseMessage();
		
		//TODO set the time initiated and status variables
		
		communicator.sendMessage(message.connectionId, responseMessage);
	}

	@Override
	public boolean acceptsMessage(Message message)
	{
		if (message.getClass().equals(ServerMessageWrapper.class))
			message = ((ServerMessageWrapper) message).internalMessage;
		
		return message.getClass().equals(PingRequestMessage.class);
	}

}
